package com.jilian.mydemo.modul.shop.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.ProductDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.NumberUtils;
import com.jilian.mydemo.utils.ScreenUtils;

import java.util.List;


public class AccessoriesShopAdapter extends RecyclerView.Adapter<AccessoriesShopAdapter.ViewHolder> {
    private Activity mContext;
    private List<ProductDto> datas;
    private CustomItemClickListener listener;

    public AccessoriesShopAdapter(Activity context, List<ProductDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_accessoriesshop, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        ViewGroup.LayoutParams  lp = holder.ivHead.getLayoutParams();
        lp.width = (ScreenUtils.getScreenWidth(mContext))/2-60;
        lp.height = (ScreenUtils.getScreenWidth(mContext))/2-40;
        holder.ivHead.setLayoutParams(lp);
        if(EmptyUtils.isNotEmpty(datas.get(position).getProductImageList())){
            Glide
                    .with(mContext)
                    .load(datas.get(position).getProductImageList().get(0))
                    .into(holder.ivHead);
        }
        holder.tvName.setText(datas.get(position).getProductName());
        holder.tvPrice.setText("￥"+ NumberUtils.forMatNumber(datas.get(position).getPrice()));


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llContainer;
        private ImageView ivHead;
        private TextView tvName;
        private TextView tvPrice;



        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            llContainer = (LinearLayout) itemView.findViewById(R.id.ll_container);
            ivHead = (ImageView)  itemView.findViewById(R.id.iv_head);
            tvName = (TextView)  itemView.findViewById(R.id.tv_name);
            tvPrice = (TextView)  itemView.findViewById(R.id.tv_price);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView,getAdapterPosition());
                }
            });
        }

    }
}
